﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TbCodigo.Enabled = false;
        TbNome.Enabled = true;
        TbLocalizacao.Enabled = true;
        DdGenero.Enabled = true;
        DdAutor.Enabled = true;
        DdAlbum.Enabled = true;
        DdFormato.Enabled = true;
        DdEspecificacao.Enabled = true;
        DdSoftware.Enabled = true;
        BtGravar.Enabled = true;
        BtCancelar.Enabled = true;
    }
    private void cancelar()
    {
        TbCodigo.Text = "";
        TbNome.Text = "";
        TbLocalizacao.Text = "";
        DdGenero.SelectedValue = "0";
        DdAutor.SelectedValue = "0";
        DdAlbum.SelectedValue = "0";
        DdFormato.SelectedValue = "0";
        DdEspecificacao.SelectedValue = "0";
        DdSoftware.SelectedValue = "0";
        TbCodigo.Enabled = true;
        TbNome.Enabled = false;
        TbLocalizacao.Enabled = false;
        DdGenero.Enabled = false;
        DdAutor.Enabled = false;
        DdAlbum.Enabled = false;
        DdFormato.Enabled = false;
        DdEspecificacao.Enabled = false;
        DdSoftware.Enabled = false;
        BtGravar.Enabled = false;
        BtCancelar.Enabled = false;
        lbErro.Text = "";

    }
    protected void BtCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }
    protected void BtGravar_Click(object sender, EventArgs e)
    {
        try
        {
            AudioBLL aBLL = new AudioBLL();
            if (TbCodigo.Text == "")
            {
                Audio a = new Audio(TbNome.Text, TbLocalizacao.Text, new Genero(Convert.ToInt32(DdGenero.SelectedValue)), new Autor(Convert.ToInt32(DdAutor.SelectedValue)), new Album(Convert.ToInt32(DdAlbum.SelectedValue)), new Formato(Convert.ToInt32(DdFormato.SelectedValue)), new Especificacao(Convert.ToInt32(DdEspecificacao.SelectedValue)), new SoftwareDeReproducao((Convert.ToInt32(DdSoftware.SelectedValue))), new Usuario(Session["usuario"].ToString()));
                aBLL.Insere(a);
            }
            else
            {
                Audio a = new Audio(Convert.ToInt32(TbCodigo.Text), TbNome.Text, TbLocalizacao.Text, new Genero(Convert.ToInt32(DdGenero.SelectedValue)), new Autor(Convert.ToInt32(DdAutor.SelectedValue)), new Album(Convert.ToInt32(DdAlbum.SelectedValue)), new Formato(Convert.ToInt32(DdFormato.SelectedValue)), new Especificacao(Convert.ToInt32(DdEspecificacao.SelectedValue)), new SoftwareDeReproducao(Convert.ToInt32(DdSoftware.SelectedValue)), new Usuario(Session["usuario"].ToString()));
                aBLL.Atualiza(a);

            }
            cancelar();
            GridView1.DataBind();
            Novo.Enabled = true;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }

    protected void Autaliza_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            AudioBLL audio = new AudioBLL();
            Audio a = audio.Consulta(id);

            TbCodigo.Enabled = false;
            TbNome.Enabled = true;
            TbLocalizacao.Enabled = true;
            DdGenero.Enabled = true;
            DdAutor.Enabled = true;
            DdAlbum.Enabled = true;
            DdFormato.Enabled = true;
            DdEspecificacao.Enabled = true;
            DdSoftware.Enabled = true;
            Novo.Enabled = false;
            BtGravar.Enabled = true;

            TbCodigo.Text = a.Codigo.ToString();
            TbNome.Text = a.Nome;
            TbLocalizacao.Text = a.Localizacao.ToString();
            DdGenero.Text = a.Genero.Codigo.ToString();
            DdAutor.Text = a.Autor.Codigo.ToString();
            DdAlbum.SelectedValue = a.Album.Cod.ToString();
            DdFormato.SelectedValue = a.Formato.Codigo.ToString();
            DdEspecificacao.SelectedValue = a.Especificacao.Codigo.ToString();
            DdSoftware.SelectedValue = a.Software.Codigo.ToString();

        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            AudioBLL audio = new AudioBLL();
            Audio a = new Audio(id);
            audio.Remove(a);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
}

